package org.xxpay.pay.channel.gepay;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xxpay.core.common.util.AmountUtil;
import org.xxpay.core.common.util.DateUtil;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.common.vo.DBApplicationConfig;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.pay.channel.PayConfig;
import org.xxpay.pay.ctrl.common.BaseController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;

/**
 * @Description: 个付通免签通道界面跳转
 * @date 2018-03-02
 * @version V1.0
 */
@Controller
public class GepayController extends BaseController {

	private static final MyLog _log = MyLog.getLog(GepayController.class);

	@Autowired
	public PayConfig payConfig;

	/**
	 * 转到前端收银台界面
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/api/gepay/cashier.htm")
	public String toCashier(HttpServletRequest request, ModelMap model) {
		JSONObject po = getJsonParam(request);

		String orderExpireTime = getString(po, "orderExpireTime"); //订单过期时间
		String payAmount = getString(po, "payAmount"); //用户支付金额
		String payOrderId = getString(po, "payOrderId"); //本系统支付订单号
		String channelId = getString(po, "channelId"); //支付渠道， 用于判断支付类型
		String qrUrl = getString(po, "qrUrl"); //二维码真实地址

		//验证参数是否合法
		if(StringUtils.isAnyEmpty(orderExpireTime, payAmount, payOrderId, channelId, qrUrl)){
			return null;
		}

		PayOrder payOrder = commonService.payOrderService.findByPayOrderId(payOrderId);
		if(payOrder == null){
			return "payment/gepay/pay_cashier";
		}

		//时间倒计时
		long timerSecond = (DateUtil.str2date(orderExpireTime).getTime() - new Date().getTime()) / 1000;
		request.setAttribute("timerSecond", timerSecond);

		model.put("payAmount", AmountUtil.convertCent2Dollar(payAmount));
		model.put("payOrderId", payOrderId);
		DBApplicationConfig dbConfig = commonService.sysConfigService.getDBApplicationConfig();
		model.put("imgSrc", dbConfig.getConfig4PayUrl() + "/qrcode_img_get?url=" + URLEncoder.encode(qrUrl) + "&width=200&height=200");

		//ws地址： ws://localhost:8088/ws/payOrder/P11100001/20180010101
		String payDomain = dbConfig.getConfig4PayUrl().replace("http://", "").replace("https://", "").replace("/api",""); //去掉http协议
		String wsProtocol = dbConfig.getConfig4PayUrl().startsWith("http://") ? "ws://" : "wss://"; //ws协议
		model.put("wsAddress", wsProtocol + payDomain + "/ws/payOrder/" + payOrderId + "/" + new Date().getTime());

		model.put("payOrderState", payOrder.getStatus());

		if("gepay_wxpay".equals(channelId)){			// 微信支付
			model.put("qrTypeCode", "wxpay");
			model.put("qrTypeName", "微信支付");

		}else if("gepay_alipay".equals(channelId)){		// 支付宝
			model.put("qrTypeCode", "alipay");
			model.put("qrTypeName", "支付宝");
		}else if("gepay_ysf".equals(channelId)){		// 云闪付
			model.put("qrTypeCode", "ysf");
			model.put("qrTypeName", "云闪付");
		}

		return "payment/gepay/pay_cashier";
	}

}
